﻿
; (function ($) {
    var context = {};
    var methods = {///添加弹出窗口明细
        addDetail: function (fun) {
            context = {};
            var detailGrid = $(this).parents("[dataJson]");
            context.grid=detailGrid;///列表对象
            var detailConfig = JSON.parse(decodeURIComponent(detailGrid.attr("dataJson")));
            context.config= detailConfig;//列表配置弹出参数对象
            context.operatetype="add";
            context.controltype="detail";
            context.$this=$(this);
            GetFromHtml('/FromTemp/GetFromTempContext?FromTempID=' +   context.config.RefControlID,undefined, function (smartFrom, fromdata) {
                $(detailGrid).wmpDetail("add_row", fromdata);
            });
        },
        editDetail: function () {
            context = {};
            var detailGrid = $(this).parents("[dataJson]");
            context.grid=detailGrid; ///列表对象
            var detailConfig = JSON.parse(decodeURIComponent(detailGrid.attr("dataJson")));
            context.config= detailConfig;///列表配置弹出参数对象
            var trobj = $(this).parents("tr");
            context.row=trobj;////修改行对象
            var rowdata = detailGrid.wmpDetail("get_row", trobj);
            context.rowdata=rowdata;///修改行数据对象
            context.operatetype="edit";
            context.controltype="detail";
            context.$this=$(this);
            GetFromHtml('/FromTemp/GetFromTempContext?FromTempID=' +   context.config.RefControlID,
                function (smartFrom) {
                smartFrom.SmartFromData("setdata", rowdata);
                return rowdata;
            }, function (smartFrom, fromdata) {
                $(detailGrid).wmpDetail("edit_row", trobj, fromdata);
            });
        },
        viewDetail: function () {
            context = {};
            var detailGrid = $(this).parents("[dataJson]");
            context.grid=detailGrid; ///列表对象
            var detailConfig = JSON.parse(decodeURIComponent(detailGrid.attr("dataJson")));
            context.config= detailConfig;///列表配置弹出参数对象
            var trobj = $(this).parents("tr");
            context.row=trobj;////修改行对象
            var rowdata = detailGrid.wmpDetail("get_row", trobj);
            context.rowdata=rowdata;///修改行数据对象
            context.operatetype="view";
            context.controltype="detail";
            context.$this=$(this);
            GetFromHtml('/FromTemp/GetFromTempContext?FromTempID=' +   context.config.RefControlID,function (smartFrom) {
                smartFrom.SmartFromData("setdata", rowdata);
                smartFrom.SmartFromData("readonly");
            });
        },
        addFrom:function(){
            context = {};
            var detailGrid = $(this).parents("[dataJson]");
            context.grid=detailGrid;
            var detailConfig = JSON.parse(decodeURIComponent(detailGrid.attr("dataJson")));
            context.config= detailConfig;
            context.operatetype="add";
            context.controltype="grid";
            context.$this=$(this);
            GetFromHtml('/From/GetFromTempContext?FromID=' +   context.config.RefControlID);
        },
        editFrom: function () {
            context = {};
            var detailGrid = $(this).parents("[dataJson]");
            context.grid=detailGrid;
            var detailConfig = JSON.parse(decodeURIComponent(detailGrid.attr("dataJson")));
            context.config= detailConfig;
            var trobj = $(this).parents("tr");
            context.row=trobj;////修改行对象
            var rowdata = detailGrid.wmpDetail("get_row", trobj);
            context.rowdata=rowdata;///修改行数据对象
            context.operatetype="edit";
            context.controltype="grid";
            context.$this=$(this);
            GetFromHtml('/From/GetFromTempContext?FromID=' +   context.config.RefControlID);
        },
        viewFrom: function () {
            context = {};
            var detailGrid = $(this).parents("[dataJson]");
            context.grid=detailGrid; ///列表对象
            var detailConfig = JSON.parse(decodeURIComponent(detailGrid.attr("dataJson")));
            context.config= detailConfig;///列表配置弹出参数对象
            var trobj = $(this).parents("tr");
            context.row=trobj;////修改行对象
            var rowdata = detailGrid.wmpDetail("get_row", trobj);
            context.rowdata=rowdata;///修改行数据对象
            context.operatetype="view";
            context.controltype="grid";
            context.$this=$(this);
            GetFromHtml('/From/GetFromTempContext?FromID=' +   context.config.RefControlID);
        },
        openfrom: function (fromId, fromName,param) {
            context = {};
            context.config={};
            context.config.RefControlID=fromId;
            context.config.RefControlName=fromName;
            context.$this=$(this);
            context.operatetype="open";
            context.param=param;
            GetFromHtml('/FromTemp/GetFromTempContext?FromTempID=' +   context.config.RefControlID);
        }
    }
    ////fun 窗体打开时调用 savefun点击保存时调用
    function GetFromHtml(ajaxTempUrl,loadfun,savefun) {
        AJAXAPI(ajaxTempUrl, {}, function (d) {
            if (d.success) {
                parent.layer.open({
                    type: 1,
                    title: '<span>' + context.config.RefControlName + '</span>',
                    content: ' <div class="layui-content"></div> <div class="layui-footer"><button type="button" class="btn btn-primary SmartFromSave">确定</button><button type="button" class="btn btn-default SmartFromClose" >关闭</button></div>',
                    anim: 'up',
                    success: function (layero, index) {
                        var smartFrom = $(layero);
                        context.LayerFrom=smartFrom;
                        context.close=function(){
                            parent.layer.close(context.LayerFrom.attr("Index"));
                            if(context.controltype=="grid"&& context.operatetype!="view"){
                                context.grid.gridList().InitPage();
                            }
                        }
                        smartFrom.find(".layui-content").append(decodeURIComponent(d.data.fromtempcontext));
                        parent.dialogResize(smartFrom, 1200, 800);
                        smartFrom.SmartFromData("init", d.data.nodepowerjson);
                        var fromId = (d.data.fromid+"").replace(/-/g, "");
                        ///加载之前的事件
                        var smartFrombeforeInitData = undefined;
                        eval("if(typeof(beforeInitData" + fromId + ")!='undefined')smartFrombeforeInitData=beforeInitData" + fromId + ";");
                        if (typeof (smartFrombeforeInitData) == "function") {
                            smartFrombeforeInitData(context);
                        }
                        var LoadSmartFromdata=undefined;
                        if (typeof (loadfun) == "function") {
                            LoadSmartFromdata= loadfun(context);
                        }
                        eval("if(typeof(LoadSmartFromdata" + fromId + ")!='undefined')LoadSmartFromdata=LoadSmartFromdata" + fromId + ";");
                        var smartFromdata;
                        if(LoadSmartFromdata!=undefined){
                            smartFromdata = LoadSmartFromdata(context);
                        }

                        ///数据组装之后的事件
                        var smartFromafterInitData = undefined;
                        eval("if(typeof(afterInitData" + fromId + ")!='undefined')smartFromafterInitData=afterInitData" + fromId + ";");
                        if (typeof (smartFromafterInitData) == "function") {
                            smartFromafterInitData(context, smartFromdata);
                        }
                        if(context.param!=undefined&&context.param!=null&& typeof (context.param.loadfun)=="function"){
                            context.param.loadfun(context);
                        }
                        smartFrom.find(".SmartFromSave").click(function () {
                            if (!smartFrom.SmartFromData("bizformdata", false, d.data.nodepowerjson)) {
                                return;
                            }
                            ///数据验证事件
                            var smartFrombizFormdata = undefined;
                            eval("if(typeof(bizFormdata" + fromId + ")!='undefined')smartFrombizFormdata=bizFormdata" + fromId + ";");
                            if (typeof (smartFrombizFormdata) == "function") {
                                if (smartFrombizFormdata(context) == false) {
                                    return;
                                }
                            }
                            var fromdata = smartFrom.SmartFromData("getdata");
                            ///数据组装之后的事件
                            var smartFromAssembleDataAfter = undefined;
                            eval("if(typeof(assembleDataAfter" + fromId + ")!='undefined')smartFromAssembleDataAfter=assembleDataAfter" + fromId + ";");
                            if (typeof (smartFromAssembleDataAfter) == "function") {
                                if (smartFromAssembleDataAfter(context, fromdata) == false) {
                                    return;
                                }
                            }
                            var SmartFromSaveData=undefined;
                            eval("if(typeof(SmartFromSaveData" + fromId + ")!='undefined')SmartFromSaveData=SmartFromSaveData" + fromId + ";");
                            if (typeof (SmartFromSaveData) == "function") {
                                SmartFromSaveData(context, fromdata);
                            }
                            if (typeof (savefun) == "function") {
                                savefun(context, fromdata);
                                parent.layer.close(smartFrom.attr("Index"));
                            }
                            if(context.param!=undefined&&context.param!=null&& typeof (context.param.savefun)=="function"){
                                context.param.savefun(context, fromdata);
                            }
                        });
                        smartFrom.find(".SmartFromClose").click(function () {
                            parent.layer.close(smartFrom.attr("Index"));
                        });
                    }
                });
            } else {
                ShowError(d.msg);
            }
        });
    }
    $.fn.OpenSmartForm = function (method) {
        if (window != window.parent && typeof (window.parent.$("#").OpenSmartForm) == "function") {
            if (arguments.length > 1) {
                return window.parent.$(this).OpenSmartForm(method, arguments[1],arguments[2],arguments[3]);
            }
            else {
                return window.parent.$(this).OpenSmartForm(method);
            }
        } else {
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method' + method + 'does not exist');
            }
        }
    };
})($);
